function GenFieldList(add,remove)
{
	var restext = '<select id="FieldType" name="select"  class="textbox-assignprofiledropdown">';
	restext += '<option value="0">Field (Show Default)</option>';
	restext += '</select>';
	if(add)
	{
		restext += '<div onclick="AddChoise()" class="bt_pluseAddChoice">&nbsp;</div>';
	}
	if(remove)
	{
		restext += '<div onclick="RemoveChoise(event)" class="bt_pluseRemoveChoice">&nbsp;</div>';
	}
	return restext;
}
function GenFieldProfile()
{
	var restext = '<select id="FieldType" name="select" class="textbox-profilefield">';
	restext += '<option value="0">Field (Show Default)</option>';
	restext += '</select>';
	return restext;
}
function GenMedicalSchooldProfile()
{
	var restext = '<select id="FieldType" name="select" class="textbox-profilefield">';
	restext += '<option>Medical School</option>';
	restext += '</select>';
	return restext;
}

function GenInteresField(add,remove)
{
	var restext = '<input id="InterestField" name="textfield4" type="text" class="textbox-interestfield" value="Interest(Show Default)" />';
	if(add)
	{
		restext += '<div onclick="AddInteresField()" class="bt_pluseAddChoice">&nbsp;</div>';
	}
	if(remove)
	{
		restext += '<div onclick="RemoveChoise(event)" class="bt_pluseRemoveChoice">&nbsp;</div>';
	}
	return restext;
}

function GenTime(index)
{
	var restext = '<select id="forumTimer' + index + '" name="select"  class="textbox-assignprofileTime">';
	var timecount = 0;
	var minuit = "00";
	for( var i = 0;timecount < 24;i++)
	{
		restext += '<option>' + timecount + ':' + minuit + '</option>';
		if(minuit == "30")
		{
			minuit = "00";
			timecount += 1;
		}
		else
		{
			minuit = '30';
		}
	}
	restext += '</select>';
	return restext;
}

function GenDay(index)
{
	var restext = '<select id="forumDay' + index + '" name="select"  class="textbox-assignDay">';
	var date = new Date();
	var day = date.getDate();
	restext += '<option>' + day + '</option>';
	for( var i = 1;i <= 31;i++)
	{
		restext += '<option>' + i + '</option>';
	}
	restext += '</select>';
	return restext;
}

function GenYear(index)
{

	var restext = '<select id="forumYear' + index + '" name="select"  class="textbox-assignYear">';

	var date = new Date();
	var nowyear = date.getYear() - 100;
	restext += '<option>' + nowyear + '</option>';

	for( var i = 1;i <= 20;i++)
	{
		var year = nowyear + i;
		restext += '<option>' + year + '</option>';
	}
	restext += '</select>';
	return restext;

}

function GenMonth(index)
{
	var restext = '<select id="forumMonth' + index + '" name="select"  class="textbox-assignDay">';
	var date = new Date();
	var month = date.getMonth() + 1;
	restext += '<option>' + month + '</option>';
	for( var i = 1;i <= 12;i++)
	{
		restext += '<option>' + i + '</option>';

		/*
		 * // restext+='<option>January</option>'; // restext+='<option>Febuary</option>'; //
		 * restext+='<option>March</option>'; // restext+='<option>April</option>'; //
		 * restext+='<option>May</option>'; // restext+='<option>June</option>'; //
		 * restext+='<option>July</option>'; // restext+='<option>August</option>'; //
		 * restext+='<option>September</option>'; // restext+='<option>October</option>'; //
		 * restext+='<option>November</option>'; // restext+='<option>December</option>';
		 */
	}

	restext += '</select>';
	return restext;
}

function GenHour(index)
{
	var restext = '<select id="forumTimerHour' + index + '" name="select"  class="textbox-assignprofileTime">';
	var timecount = 0;
	var minuit = 0;
	for( var i = 0;timecount < 24;i++)
	{
		restext += '<option>' + timecount + ':' + minuit + '</option>';
		if(minuit == "30")
		{
			minuit = "00";
			timecount += 1;
		}
		else
		{
			minuit = '30';
		}
	}
	restext += '</select>';
	return restext;
}

function GenDater(index)
{
	return GenDay(index) + ' ' + GenMonth(index) + ' ' + GenYear(index);
}

function GenTimer(index)
{

}

function PostForum()
{
	if(this.WaitPostForum && this.WaitPostForum == true)
		return;
	this.WaitPostForum = true;

	var textImageList = new Array();
	var imageList = new Array();
	var count = 0;

	for( var i = 0;i < NumOfRowImage;i++)
	{
		if(document.getElementById('despic' + i) != null)
		{
			textImageList[count] = document.getElementById('despic' + i).value;
			imageList[count] = imageURLList[i];
			count += 1;
		}
	}

	// imageList Image Lists
	// textImageList Text Lists
	var Topic = document.getElementById("PostTopic").value;
	var Content = document.getElementById("TextFieldForm").value;
	var FieldType = document.getElementById("FieldType").value;

	HttpAsyncRequest("POST","/content?type=Forum","{ Topic : '" + Topic + "' , Content : '" + Content + "' , FieldType : '" + FieldType + "' }",function(responseText)
	{
		this.WaitPostForum = false;
		if(responseText != null && responseText.length > 0)
			alert("POST : " + responseText);

		document.getElementById("forumNavForm").setAttribute("class","navsubmenu");

		LoadAll();
	},function(statusText)
	{
		this.WaitPostForum = false;
		alert("POST Error : " + statusText);
	});
}

var navFormID = [ "forumNavForm","newsNavForm","eventNavForm","jobNavForm" ];
function Toggle(id)
{
	var i = 0;
	while(i < navFormID.length)
	{
		var navForm = document.getElementById(navFormID[i]);

		if(id == navFormID[i] && navForm.getAttribute("class") == "navsubmenu")
			navForm.setAttribute("class","navsubmenu2");
		else navForm.setAttribute("class","navsubmenu");

		i++;
	}
}

Date.prototype.lastDate = function()
{
	var d = new Date(this.getFullYear(),this.getMonth(),32);
	return 33 - d.getDate();
};
function SetDate(event,date)
{
	var element = event.currentTarget;
	if(element)
	{
		if(date)
			element.form.dateTime.setDate(date);
		SetDateField(element.form);
	}
}

function SetMonth(event)
{
	var element = event.currentTarget;
	if(element)
	{
		var form = element.form;
		var index = form["Month"].selectedIndex;
		var selected = form["Month"].options[index];
		form.dateTime.setFullYear(selected.getAttribute("year"),selected.getAttribute("month"),1);
		SetDateField(form);
	}
}
function increaseMonth(event)
{
	var element = event.currentTarget;
	if(element)
	{
		var form = element.form;
		form["Month"].selectedIndex++;
		SetMonth(event);
	}
}
function decreaseMonth(event)
{
	var element = event.currentTarget;
	if(element)
	{
		var form = element.form;
		form["Month"].selectedIndex--;
		SetMonth(event);
	}
}

function CloseSetTime(event)
{
	var element = event.currentTarget;
	if(element)
	{
		var form = element.form;
		SetDateField(form);

		form["Text"].setAttribute("readonly","readonly");
	}
}

function SetTime(event)
{
	var element = event.currentTarget;
	if(element)
	{
		var form = element.form;
		SetDateField(form);
	}
}

var months = [ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" ];
function SetDateField(form)
{
	var selectedIndex = form["Time"].selectedIndex;
	var selected = form["Time"].options[selectedIndex];
	form.dateTime.setHours(selected.getAttribute("hour"),selected.getAttribute("minute"),0);

	form["Text"].value = form.dateTime.toDateString();

	var m = form.dateTime.getMonth();
	var y = form.dateTime.getFullYear();
	var option = "";
	var x = 0;
	while(x < 13)
	{
		var z = (x + m) - 6;
		if(z < 0)
		{
			z = 12 + z;
			option += "<option month='" + z + "' year='" + (y - 1) + "'>" + months[z] + " : " + (y - 1) + "</option>";
		}
		else if(z > 11)
		{
			z = z - 12;
			option += "<option month='" + z + "' year='" + (y + 1) + "'>" + months[z] + " : " + (y + 1) + "</option>";
		}
		else option += "<option month='" + z + "' year='" + y + "'>" + months[z] + " : " + y + "</option>";
		x++;
	}

	form["Month"].innerHTML = option;
	form["Month"].selectedIndex = 6;

	var firstDate = new Date(form.dateTime);
	firstDate.setDate(1);

	var dayGrid = "<tr>";
	var i = 0;
	while(i < firstDate.getDay())
	{
		dayGrid += "<td></td>";
		i++;
	}

	var j = 1;
	while(j < firstDate.lastDate())
	{
		dayGrid += "<td><button type='button' name='date' onclick='SetDate(event,\"" + j + "\")'>" + j + "</button></td>";
		j++;
		i++;
		if(i >= 7)
		{
			dayGrid += "</tr><tr>";
			i = 0;
		}
	}

	form.getElementsByTagName("tbody")[0].innerHTML = dayGrid + "</tr>";
}

function TogglePicker(event)
{
	var element = event.currentTarget;
	if(element && element.form)
	{
		if(!element.form.dateTime)
			element.form.dateTime = new Date();

		element.removeAttribute("readonly");
		SetDateField(element.form);
	}
}

function genTypeWork()
{
	text = '<select name="select" id="WorkType" class="textbox-assignprofiledjob"><option value="0">Type of work</option></select>';
	return text;
}
function genPayRate()
{
	text = '<select name="select" id="RatePay" class="textbox-assignprofiledjob"><option value="0">Rate of payment</option></select>';
	return text;
}

var JobType = 1;
function JobTab(tab)
{
	switch(tab)
	{
		case 1 :
			document.getElementById("tab1").style.backgroundColor = "#999999";
			document.getElementById("tab2").style.backgroundColor = "#47444a";
		break;
		case 2 :
			document.getElementById("tab2").style.backgroundColor = "#999999";
			document.getElementById("tab1").style.backgroundColor = "#47444a";
		break;
	}
}

function addLink()
{
	var table = document.getElementById("addingarea");
	table.rows[0].cells[0].innerHTML = '<input name="textfield2" type="text" class="textbox-linkforumform" value="Your URL" /> <div onclick="addLinkToMessage()" class="addbutton">Add</div>';
}
function addLinkToMessage()
{
	var table = document.getElementById("addingarea");
	table.rows[0].cells[0].innerHTML = '';
}
function AddChoise()
{
	var tblBody = document.getElementById('navigatorchoice').tBodies[0];
	var newRow = tblBody.insertRow(-1);
	newRow.innerHTML = GenFieldList(false,true);
}
function AddInteresField()
{
	var tblBody = document.getElementById('navigatorchoice').tBodies[0];
	var newRow = tblBody.insertRow(-1);
	newRow.innerHTML = GenInteresField(false,true);
}
function RemoveChoise(e)
{
	var evt = e || window.event; // this assign evt with the event object
	var current = evt.target || evt.srcElement; // this assign current with the
	// event target
	while((current = current.parentElement) && current.tagName != "TR")
		;
	current.parentElement.removeChild(current);
}

var NumOfRowImage = 0;
var imageURLList = new Array();
function addImage()
{
	document.getElementById('addingareaImage').fading(200,0,100);
	imageURLList[NumOfRowImage] = 'imgs/previewimage.gif';
	var textDes = 'Field (Show default)';
	var tblBody = document.getElementById('addingareaImage').tBodies[0];
	var newRow = tblBody.insertRow(-1);
	var textHtml = '<div style="padding:1px 0px 1px 0px"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="123"><img onclick="deleteImage(event)" src="' + imageURLList[NumOfRowImage] + '" width="123" height="80" /></td><td width="382"><textarea id="despic' + NumOfRowImage + '" name="textfield2" class="textbox-fieldimageforumform">' + textDes + '</textarea></td></tr></table></div>';

	if(detect_browser() == "IE")
	{
		var newCell0 = newRow.insertCell(0);
		newCell0.innerHTML = textHtml;
	}
	else
	{
		newRow.innerHTML = textHtml;
	}
	NumOfRowImage += 1;
}

function deleteImage(e)
{
	var evt = e || window.event; // this assign evt with the event object
	var current = evt.target || evt.srcElement; // this assign current with the
	// event target
	// here we will delete the line
	while((current = current.parentElement) && current.tagName != "TR")
		;
	current.parentElement.removeChild(current);

	document.getElementById('addingareaImage').fading(200,0,100);
}

function detect_browser()
{
	var mybrowser = navigator.userAgent;
	mybs = 'Unknow';
	if(mybrowser.indexOf('MSIE') > 0)
	{
		mybs = "IE";
	}
	if(mybrowser.indexOf('Firefox') > 0)
	{
		mybs = "Firefox";
	}
	if(mybrowser.indexOf('Presto') > 0)
	{
		mybs = "Opera";
	}
	if(mybrowser.indexOf('Chrome') > 0)
	{
		mybs = "Chrome";
	}
	return mybs;
}

function ShowProfileForm()
{
	var formtext = '<table width="100%" border="0" cellspacing="0" cellpadding="2"><tr><td align="left" valign="top"><div style="padding-bottom:10px"><img src="imgs/Profile.png" width="40" height="40" /></div></td></tr><tr><td align="left" valign="top"><div style="padding-bottom:10px"><div onclick="AddImageProfile()" class="crossbutton">Add Image</div></div></td></tr><tr><td align="left" valign="top"><span class="White-text">Fullname</span></td></tr><tr><td align="left" valign="top"><span class="White-text">Medical School</span></td></tr><tr><td align="left" valign="top"><span class="White-text">Hospital Internship</span></td></tr><tr><td align="left" valign="top"><span class="White-text">Resident training school</span></td></tr><tr><td align="left" valign="top">' + GenFieldProfile() + '</td></tr><tr><td align="left" valign="top"><span class="White-text">Email</span></td></tr><tr><td align="left" valign="top"><span class="White-text">Facebook</span></td></tr><tr><td align="left" valign="top"><span class="White-text">Twitter</span></td></tr><tr><td align="left" valign="top"><span class="White-text">Website</span></td></tr><tr><td align="left" valign="top"><span class="White-text">Bio</span></td></tr><tr><td align="left" valign="top"><textarea id="TextFieldForm" name="textfield" class="textbox-bioform">Your Message</textarea></td></tr><tr><td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="61%">&nbsp;</td><td width="39%"><div onclick="SaveProfile()" class="normalbutton">Save</div></td></tr></table></td></tr></table>';
	var table = document.getElementById("profileform");
	table.rows[0].cells[0].innerHTML = formtext;

}

function ShowTagForm()
{
	var formtext = '<table width="100%" border="0" cellspacing="0" cellpadding="3"><tr><td align="left" valign="top"><textarea name="textfield" class="textbox-tagform" id="textfield"></textarea></td></tr><tr><td align="left" valign="top">' + GenMedicalSchooldProfile() + '</td></tr><tr><td align="left" valign="top">' + GenFieldProfile() + '</td></tr><tr><td height="75" align="left" valign="top">&nbsp;</td></tr><tr><td align="left" valign="top">&nbsp;</td></tr><tr><td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="61%">&nbsp;</td><td width="39%"><div onclick="SaveProfile()" class="normalbutton">Save</div></td></tr></table></td></tr></table>';
	var table = document.getElementById("tagform");
	table.rows[0].cells[0].innerHTML = formtext;

}

function ToMain()
{
	var readContent = document.getElementById("readContent");
	var allContent = document.getElementById("allContent");
	if(allContent.style.display == "none")
	{
		allContent.style.display = "block";
		readContent.style.display = "none";
	}

	var panel = document.getElementById(readContent.currentPanel);
	var allDiv = allContent.getElementsByTagName("div");
	for( var i = 0;i < allDiv.length;i++)
	{
		var div = allDiv.item(i);
		if(div instanceof HTMLDivElement && div.className == "contenttab" && div.innerHTML == panel.id)
		{
			panel.parentNode.removeChild(panel);
			div.parentNode.insertBefore(panel,div.nextSibling);
		}
	}

	readContent.currentPanel = null;

	document.getElementById("btnav_home").removeAttribute("href");
}
function ToRead(id,key)
{
	document.getElementById("btnav_home").href = "javascript:ToMain()";

	var readContent = document.getElementById("readContent");
	if(document.getElementById("allContent").style.display != "none")
	{
		document.getElementById("allContent").style.display = "none";

		readContent.style.display = "block";
	}

	var panel = document.getElementById(id);
	var table = readContent.getElementsByTagName("table")[0];
	if(table instanceof HTMLTableElement)
	{
		if(panel.parentNode instanceof Element)
			panel.parentNode.removeChild(panel);

		table.rows[0].cells[0].getElementsByTagName("div")[0].innerHTML = id;
		table.rows[0].cells[0].appendChild(panel);
		readContent.currentPanel = id;
	}
	else alert("Why table is " + typeof (table) + " but not Table?");
}

function LoadForum(forum)
{
	var item = "";

	var i = 0;
	while(i < forum.length)
	{
		item += "<tr><td>";
		item += "<div class='headline'>" + forum[i].Topic + "</div>";
		item += "<div class='tag'>" + "Tag" + "</div>";
		item += "<div class='text1'>" + forum[i].Content + "</div>";
		item += "<div class='commentcounter'><a href=\"javascript:ToRead('Forum','key')\">" + "10 Comment" + "</a></div>";
		item += "<div class='readmore'><a href=\"javascript:ToRead('Forum','key')\">more</a></div>";
		item += "</td></tr>";

		i++;
	}

	document.getElementById("Forum").innerHTML = "<tbody>" + item + "</tbody>";
}

function LoadPanel(id,num)
{
	var item = "";

	var i = 0;
	while(i < num)
	{
		var text = "tex Each HTML element has an&nbsp;<em>innerHTML</em>&nbsp;property that defines both the HTML code";

		item += "<tr>";
		item += "<td width='48%' align='left' valign='top'>";
		item += "<img src='" + "imgs/image_dummy_article.jpg" + "' width='170' height='102' />";
		item += "</td>";
		item += "<td width='48%' align='left' valign='top'>";
		item += "<div class='headline'>" + "Headline" + "</div>";
		item += "<div class='tag'>" + "Tag" + "</div>";
		item += "<div class='text1'>" + text + "</div>";
		item += "<div class='readmore'><a onclick=" + +">more</a></div>";
		item += "</td>";
		item += "</tr>";

		i++;
	}

	document.getElementById(id).innerHTML = "<tbody>" + item + "</tbody>";
}

function LoadJobs()
{
	var list = "";

	var i = 0;
	while(i < 5)
	{
		var hospital = "Hospital name";
		list += "<div><span class='datejob'> 10/10/10</span>" + hospital + "</div>";
		i++;
	}

	document.getElementById("Jobs").innerHTML = list;
}

function LoadHospitals()
{
	var list = "";
	var hospitals = [ "Public Hospital","Private Hotpital","Hospital for Internship" ];

	var i = 0;
	while(i < 3)
	{
		list += "<div>" + hospitals[i] + "</div>";
		i++;
	}

	document.getElementById("Hospitals").innerHTML = list;
}

function ContentLoaded(responseText)
{
	if(!responseText || responseText.length < 1)
		return;

	var res;
	try
	{
		res = JSON.parse(responseText);
	}
	catch(e)
	{
		alert("ContentLoaded parse error : " + e + "\n" + responseText);
		return;
	}

	if(res.User)
		document.getElementById("UserBox").rows[0].cells[0].innerHTML = "Welcome," + res.User.Name;
	else alert("Why no userName?");

	if(res.Forum)
		LoadForum(res.Forum);

	LoadPanel("News",3);
	LoadPanel("Articles",3);
	LoadJobs();
	LoadHospitals();
}
function LoadAll()
{
	HttpAsyncRequest("GET","/content",null,ContentLoaded,function(statusText)
	{
		alert("LoadForum response error : " + statusText);
	});

	if(this.LoadTimeOut)
		clearTimeout(this.LoadTimeOut);
	this.LoadTimeOut = setTimeout(LoadAll,30000);
}

function GenTimeOption()
{
	var index = 0;
	while(index < document.getElementsByName("Time").length)
	{
		var selectTime = document.getElementsByName("Time").item(index);
		index++;

		if(selectTime.tagName != "SELECT")
		{
			alert("Any Element named Time must be SELECT not " + selectTime.tagName);
			continue;
		}

		var option = "";
		var i = 0;
		while(i < 24)
		{
			var hour = i > 9 ? i : "0" + i;
			option += "<option hour='" + i + "' minute='00'>" + hour + " : 00</option>";
			option += "<option hour='" + i + "' minute='30'>" + hour + " : 30</option>";
			i++;
		}
		selectTime.innerHTML = option;
		selectTime.selectedIndex = 24;
	}
}

function LogOut()
{
	document.cookie += ";expires=" + new Date(0).toUTCString();
	window.location.reload(true);
}

window.onload = function()
{
	LoadAll();

	GenTimeOption();
};
